package com.th.supcom.pds.common.client.widget;

import com.extjs.gxt.ui.client.Style.SelectionMode;
import com.extjs.gxt.ui.client.data.ModelData;
import com.extjs.gxt.ui.client.store.GroupingStore;
import com.extjs.gxt.ui.client.widget.ContentPanel;
import com.extjs.gxt.ui.client.widget.grid.CheckBoxSelectionModel;
import com.extjs.gxt.ui.client.widget.grid.ColumnModel;
import com.extjs.gxt.ui.client.widget.grid.EditorGrid;
import com.extjs.gxt.ui.client.widget.grid.Grid;
import com.extjs.gxt.ui.client.widget.grid.GridView;
import com.extjs.gxt.ui.client.widget.layout.FitLayout;

/**
 * PDS Grid
 * 
 * @author yangchong
 * @param <M>
 */
public abstract class PdsGroupGrid <M extends ModelData> extends ContentPanel
{
    private EditorGrid <M> m_grid;
    private GroupingStore <M> m_groupingStore;

    /**
     * 默认构造函数
     */
    public PdsGroupGrid ()
    {
        initUI ();
        createGrid ();
    }

    /**
     * 清除数据
     */
    public void clear ()
    {
        m_groupingStore.removeAll ();
    }

    /**
     * 创建Column Model
     * 
     * @return
     */
    public abstract ColumnModel createColumnModel ();

    /**
     * 创建Grid
     */
    private void createGrid ()
    {
        m_groupingStore = new GroupingStore <M> ();
        final CheckBoxSelectionModel <M> sm = new CheckBoxSelectionModel <M> ();
        m_grid = new EditorGrid <M> (m_groupingStore, createColumnModel ());
        m_grid.setBorders (false);
        m_grid.setLoadMask (true);
        m_grid.setColumnLines (true);

        m_grid.setSelectionModel (sm);
        m_grid.getSelectionModel ().setSelectionMode (SelectionMode.SINGLE);

        add (m_grid);
    }

    /**
     * 获取Grid
     * 
     * @return
     */
    public Grid <M> getGrid ()
    {
        return m_grid;
    }

    /**
     * 获取listStore
     * 
     * @return
     */
    public GroupingStore <M> getGroupingStore ()
    {
        return m_groupingStore;
    }

    /**
     * 初始化UI
     */
    private void initUI ()
    {
        setLayout (new FitLayout ());
        setHeaderVisible (false);
        setBorders (false);
        setBodyBorder (false);
    }

    /**
     * 设置GridView
     * 
     * @param gridView
     */
    public void setGridView (final GridView gridView)
    {
        m_grid.setView (gridView);
    }
}
